﻿<%@ WebHandler Language="C#" Class="uMESPartReportingHandler" %>

using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;
using uMES.LeanManufacturing.ReportBusiness;
using uMES.LeanManufacturing.ParameterDTO;

public class uMESPartReportingHandler : IHttpHandler, IReadOnlySessionState
{
    public void ProcessRequest(HttpContext context)
    {
        string Type = context.Request["Type"];
        if (Type == "GetOprNo")
        {
            StringBuilder result = new StringBuilder();
            uMESPartReportingBusiness bll = new uMESPartReportingBusiness();
            DataTable dt = bll.GetProcessNo();
            result.Append("{\"code\":0,\"msg\":\"查询成功\",\"data\":[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                result.Append("\"" + dt.Rows[i]["processno"].ToString() + "\"");
                if (i < dt.Rows.Count - 1)
                {
                    result.Append(",");
                }
            }
            result.Append("]}");
            //context.Response.ContentType = "application/json";
            context.Response.Write(result);
        }
        if (Type == "GetSpec")
        {
            string CurrentPageIndex = context.Request["CurrentPageIndex"];
            string FactoryId = context.Request["FactoryId"];
            context.Response.ContentType = "application/json";
            if (string.IsNullOrEmpty(CurrentPageIndex))
            {
                context.Response.Write("{\"code\":1,\"msg\":\"当前页码不能为空\",\"data\":[]}");
                return;
            }
            if (string.IsNullOrEmpty(FactoryId))
            {
                context.Response.Write("{\"code\":1,\"msg\":\"厂间不能为空\",\"data\":[]}");
                return;
            }

            int intIndex = int.Parse(CurrentPageIndex);
            string OprNo = context.Request["OprNo"];
            Dictionary<string, string> para = new Dictionary<string, string>();
            para.Add("FactoryID", FactoryId.TrimStart().TrimEnd());
            if (OprNo == "全部")
            {
                OprNo = "";
            }
            if (!string.IsNullOrEmpty(OprNo))
            {
                para.Add("ProcessNo", OprNo);
            }
            para.Add("IsCurrentFactory", context.Request["IsCurrentFactory"]);
            uMESPartReportingBusiness bll = new uMESPartReportingBusiness();
            uMESPagingDataDTO pagingDataDTO = bll.GetPartReportingMainInfo(para, intIndex, 8);
            DataSet ds = pagingDataDTO.DBset;
            StringBuilder result = new StringBuilder();
            DataTable dtContainer = ds.Tables["containerName"];
            result.Append("{\"code\":0,\"msg\":\"查询成功\",\"pageCount\":" + pagingDataDTO.PageCount + ",\"data\":[");
            for (int i = 0; i < dtContainer.Rows.Count; i++)
            {
                result.Append("{\"rowno\":\"" + dtContainer.Rows[i]["RN2"].ToString() + "\",");
                result.Append("\"oprno\":\"" + dtContainer.Rows[i]["PROCESSNO"].ToString() + "\",");
                result.Append("\"productname\":\"" + dtContainer.Rows[i]["PRODUCTNAME"].ToString() + "\",");
                result.Append("\"description\":\"" + dtContainer.Rows[i]["DESCRIPTION"].ToString() + "\",");
                result.Append("\"containerid\":\"" + dtContainer.Rows[i]["CONTAINERID"].ToString() + "\",");
                result.Append("\"containername\":\"" + dtContainer.Rows[i]["CONTAINERNAME"].ToString() + "\",");
                result.Append("\"plannedstartdate\":\"" + System.DateTime.Parse(dtContainer.Rows[i]["PLANNEDSTARTDATE"].ToString()).ToString("yyyy-MM-dd") + "\",");
                result.Append("\"plannedcompletiondate\":\"" + System.DateTime.Parse(dtContainer.Rows[i]["PLANNEDCOMPLETIONDATE"].ToString()).ToString("yyyy-MM-dd") + "\",");
                result.Append("\"conqty\":\"" + dtContainer.Rows[i]["CONQTY"].ToString() + "\",");
                if (!string.IsNullOrEmpty(dtContainer.Rows[i]["holdreasonid"].ToString()))
                {
                    result.Append("\"status\":\"暂停\",");//批次状态
                }
                else if (dtContainer.Rows[i]["status"].ToString() == "2")
                {
                    result.Append("\"status\":\"关闭\",");
                }
                else
                {
                    result.Append("\"status\":\"正常\",");
                }
                result.Append("\"specs\":[");
                string strConName = dtContainer.Rows[i]["containername"].ToString();
                DataRow[] drs = ds.Tables["detailinfo"].Select("containername='" + strConName + "'", "showno,sequenceno");
                for (int j = 0; j < drs.Length; j++)
                {
                    result.Append("{\"specid\":\"" + drs[j]["SPECID"].ToString() + "\",");
                    result.Append("\"specname\":\"" + drs[j]["SPECNAME"].ToString() + "\",");
                    result.Append("\"isquality\":\"" + drs[j]["ISQUALITY"].ToString() + "\",");//是否预警
                    result.Append("\"specstatus\":\"" + drs[j]["state"].ToString() + "\",");//加工状态,1:已派工Yellow,2:已报工YellowGreen,3:已检验DeepSkyBlue,4:已完工Green,5已接收Gold
                    result.Append("\"synerspec\":\"" + drs[j]["synerspec"].ToString() + "\"}");//是否协作工序
                    if (j < drs.Length - 1)
                    {
                        result.Append(",");
                    }
                }
                result.Append("]}");
                if (i < dtContainer.Rows.Count - 1)
                {
                    result.Append(",");
                }
            }
            result.Append("]}");
            context.Response.Write(result);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}